<%--
  Created by IntelliJ IDEA.
  User: 帅帅的奇奇
  Date: 2019/5/17
  Time: 11:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
    <head>
        <title>Title</title>
        <script src="../../js/jquery-3.3.1.min.js" charset="utf-8"></script>
        <script src="../../js/cookie/jquery.cookie.js" charset="utf-8"></script>
        <script src="../../layui-v2.2.5/layui/layui.js" charset="utf-8"></script>
        <link rel="stylesheet" href="../../layui-v2.2.5/layui/css/layui.css" media="all">
    </head>
<body>

<form class="layui-form" id="myForm" action="###">
    <input type="hidden" name="id" value="${id}">

    <div class="layui-form-item">
        <label class="layui-form-label">优惠券</label>
        <div class="layui-input-block">
            <select name="couponId" lay-filter="aihao">
                <option value=""></option>
                <c:forEach items="${list}" var="co">
                    <option value="${co.id}">${co.name}</option>
                </c:forEach>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="demo1">设置关联</button>
        </div>
    </div>

</form>
<script src="/layui-v2.4.5/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<form class="layui-form" action="" >
    <table class="layui-table">
        <colgroup>
            <col width="100">
            <col width="100">
            <col width="100">
            <col width="100">
            <col>
        </colgroup>
        <thead>
        <tr>
            <th>名称</th>
            <th>价值金额</th>
            <th>售卖价格</th>
            <th>有效期开始时间</th>
            <th>有效期结束时间</th>
            <th>备注</th>
            <th>创建时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${pageUtil.list}" var="rl">
            <tr>
                <td>${rl.name}</td>
                <td>${rl.money}</td>
                <td>${rl.price}</td>
                <td>${rl.startTime}</td>
                <td>${rl.endTime}</td>
                <td>${rl.remark}</td>
                <td>${rl.createTime}</td>
                <td><button type="button" class="layui-btn layui-btn-danger" onclick="delProduct('${record.ticketCouponId}')">删除</button></td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</form>
<div id="under" style="margin-top: 20rem"></div>

</div>
<script>
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });
        //日期时间选择器
        laydate.render({
            elem: '#test1'
            ,type: 'datetime'
        });
        //日期时间选择器
        laydate.render({
            elem: '#test2'
            ,type: 'datetime'
        });

        //时间范围
        laydate.render({
            elem: '#test9'
            ,type: 'time'
            ,range: true
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 5){
                    return '标题至少得5个字符啊';
                }
            }
            ,pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });

        //监听指定开关
        form.on('switch(switchTest)', function(data){
            layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });

        //监听提交
        form.on('submit(demo1)', function(data){
            // layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // })
            // return false;

            $.ajax({
                url: '/shop/shoplinkCoupon',
                type: 'POST', //GET
                async: true,    //或false,是否异步
                data: $("#myForm").serialize(),
                timeout: 5000,    //超时时间
                dataType: 'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend: function (xhr) {
                    /*console.log(xhr)
                     console.log('发送前')*/
                },
                success: function (data) {
                    console.log(data);
                     if(data.code=='0') {
                         layer.msg("关联成功");
                         window.location.reload();
                     }
                    // }else {
                    //     layer.msg("保存失败")
                    // }

                },
                error: function (xhr, textStatus) {
                    console.log('错误')
                    console.log(xhr)
                    console.log(textStatus)
                },
                complete: function () {
                    //console.log('结束')
                }
            });

            return false;//禁止跳转，否则会提交两次，且页面会刷新

        });

        //表单初始赋值
        form.val('example', {
            "username": "贤心" // "name": "value"
            ,"password": "123456"
            ,"interest": 1
            ,"like[write]": true //复选框选中状态
            ,"close": true //开关状态
            ,"sex": "女"
            ,"desc": "我爱 layui"
        })
        /*删除商品*/
    });
    function delProduct(id) {
        $.ajax({
            url:'/shop/delCoupon',
            type:'POST', //GET
            async:true,    //或false,是否异步
            data:{
                id:id,
            },
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            beforeSend:function(xhr){
                /*console.log(xhr)
                 console.log('发送前')*/
            },
            success:function(dataA){
                console.log(dataA);
                if (dataA.code=='000000'){
                    window.location.reload();
                    // 获得frame索引
                }else {
                    layer.msg("删除失败",{time:1000,icon: 5});
                }
            },
            error:function(xhr,textStatus){
                console.log('错误')
                console.log(xhr)
                console.log(textStatus)
            },
            complete:function(){
                //console.log('结束')
            }
        })
    }
</script>
</body>
</html>

